﻿using System;
using System.Collections.Generic;
using System.Text;
using Rison.DataAccess.Common;
using Rison.DBHelper;
using System.Data;


namespace Rison.DataAccess.Data.NRBZ
{
    public sealed class NrbzDb : DataAccessBase
    {
        protected override string tableName
        {
            get { return "NRBZ_Master"; }
        }

        public string TableSubName
        {
            get { return "NRBZ_Sub"; }
        }

        public DataRow GetInformation(string id)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select * from {0} where id='{1}'", tableName, id);
            return base.ExecuteSqlToDataRow(sql.ToString());
        }

        public DataRow GetInformation(string departid, string year)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select * from {0} where departid='{1}' and year ='{2}'", tableName, departid, year);

            return base.ExecuteSqlToDataRow(sql.ToString());
        }

        public int UpdateValue(string sqlstring,string id)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("update {0} Set ", tableName);
            sql.Append(sqlstring);
            sql.AppendFormat(" where id='{0}' ", id);

            return base.ExecuteNonSql(sql.ToString());
        }

        #region "子表信息"

        public DataSet GetSubInfoByParentID(string parentId,string type)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select * from {0} where ExamID='{1}' and type='{2}' order by ordername", TableSubName, parentId,type);
            return base.ExecuteSql(sql.ToString(), "GetSubInfoByParentID");
        }

        /// <summary>
        /// 删除子表信息。
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int DeleteSubInfobyParentId(string parentId,string type)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("delete from {0} where examid='{1}' and type='{2}' and ifshow=0", TableSubName, parentId.ToString(), type.ToString());

            return base.ExecuteNonSql(sql.ToString());
        }


        /// <summary>
        /// 插入子表信息。
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int InsertSubInfobyParentId(string field,string Param)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("insert into {0} ({1}) values ({2})", TableSubName,field, Param);

            return base.ExecuteNonSql(sql.ToString());
        }

        public DataSet GetSubInfoCountbyShow(string parentId, string type)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select Var1 from {0} where ExamID='{1}' and type='{2}' and ifshow=1", TableSubName, parentId, type);
            return base.ExecuteSql(sql.ToString(), "GetSubInfoCountbyShow");
        }

        #endregion

        #region "保存附件"
        public int SaveAttachment(string sqlString)
        {
            return base.ExecuteNonSql(sqlString);
        }

        public DataSet LoadAttachement(string sqlString)
        {
            return base.ExecuteSql(sqlString, "attachment");
        }

        public int DeleteAttachment(string sqlString)
        {
            return base.ExecuteNonSql(sqlString);
        }
        #endregion
    }
}
